<?php

namespace app\common\model\product;

use app\common\model\user\Address;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'product_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'pay_type_text',
        'refreshtime_text',
        'status_text',
        'image_str',
    ];


    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }

    public function getPlantList()
    {
        return ['0' => __('Plant 0'), '1' => __('Plant 1')];
    }

    public function getPayTypeList()
    {
        return ['0' => __('Pay_type 0'), '1' => __('Pay_type 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }



    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefreshtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refreshtime']) ? $data['refreshtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setRefreshtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function member()
    {
        return $this->belongsTo('Member', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function yuOrder($id,$address_id,$uid)
    {
        $goods = new Member();
        $goods = $goods->get($id)->toArray();
        if (isset($address_id) && !empty($address_id)) {
            $addressInfo = Address::get($address_id);
            $goods['addressInfo'] = $addressInfo;
            $goods['address_id']  = $address_id;
        } else {
            $addressInfo = Address::getDefaultAddress($uid);
            if ($addressInfo) {
                $goods['addressInfo'] = $addressInfo;
                $goods['address_id']  = $addressInfo['id'];
            }
        }
        return $goods;
    }
    public  function addData($data,  $uid)
    {

        $array = [
            'user_id' => $uid,
            'name' =>  $data['title'],
            'image' =>  $data['image'],
            'price' =>  $data['price'],
            'sn' => sn('product_order', 'sn', 'PO'),
            'pay_type' => $data['pay_type'],
            'goods_id' => $data['id'] ?? 0,
        ];
        if (isset($address_id) && !empty($address_id)) {
            $addressInfo = Address::get($address_id);
            $array['address_id']  = $address_id;
            $address =  $addressInfo['province'] . $addressInfo['city'] . $addressInfo['district'] . $addressInfo['address'];
            $array['address'] = $address;
            $array['consignee'] = $addressInfo['contact'];
            $array['phone'] = $addressInfo['telephone'];
        } else {
            $addressInfo = Address::getDefaultAddress($uid);
            if ($addressInfo) {
                $array['address_id']  = $addressInfo['id'];
                $address =  $addressInfo['province'] . $addressInfo['city'] . $addressInfo['district'] . $addressInfo['address'];
                $array['address'] = $address;
                $array['consignee'] = $addressInfo['contact'];
                $array['phone'] = $addressInfo['telephone'];
            }
        }
        $this->allowField(true)->save($array);
        return $this->id;
    }
}
